﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ServiceModel;
using System.Text;
using Controllayer;
using Modellayer;
using System.IO;
using System.Xml;
using System.Threading;
using System.Globalization;

namespace BioBookWcf
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BioBookService" in both code and config file together.
    public class BioBookService : IBioBookService
    {
        private PersonCtr pCtr;
        private ReservationCtr resCtr;
        private ShowCtr sCtr;
        private ShowRoomCtr shCtr;

        public BioBookService()
        {
            pCtr = new PersonCtr();
            resCtr = new ReservationCtr();
            sCtr = new ShowCtr();
            shCtr = new ShowRoomCtr();
        }

        public void createEmployee(int phoneNumber, string email, string name, string password, string loginId)
        {
            pCtr.createEmployee(phoneNumber, email, name, password, loginId);
        }

        public void createCustomer(int phoneNumber, string email, string name, string password)
        {
            pCtr.createCustomer(phoneNumber, email, name, password);
        }

        public Person findPerson(int phoneNumber)
        {
            return pCtr.findPerson(phoneNumber);
        }

        public void createShow(Movie movie, ShowRoom showroom, int showId, string sTime)
        {
            sCtr.createShow(movie, showroom, showId, sTime);
        }
        public Show findShow(int showId)
        {
            //Show s = sCtr.findShow(showId);
            //s.seats = null;
            return sCtr.findShow(showId);
        }

        public void createMovie(string Title, string Movielength, string MovieDescription)
        {
            sCtr.createMovie(Title, Movielength, MovieDescription);
        }

        public Movie findMovie(string Title)
        {
            return sCtr.findMovie(Title);
        }

        public void createShowRoom(int showRoomNumber, int numberOfSeats, string showRoomDescription)
        {
            shCtr.createShowRoom(showRoomNumber, numberOfSeats, showRoomDescription);
        }

        public ShowRoom findShowRoom(int showRoomNumber)
        {
            return shCtr.findShowRoom(showRoomNumber);
        }

        public ShowRoom updateShowRoom(int showRoomNumber, int numberOfSeats, string showRoomDescription)
        {
            return shCtr.updateShowRoom(showRoomNumber, numberOfSeats, showRoomDescription);
        }

        public Show updateShow(Show show)
        {
            CultureInfo ci = new CultureInfo("en-GB");
            Thread.CurrentThread.CurrentCulture = ci;
            return sCtr.updateShow(show);
        }

        public void createReservation(Person p, Show show, List<Seat> seats, int reservationsID)
        {
            //List<Seat> seat = new List<Seat>();
            //foreach (Seat s in seats)
            //{
            //    seat.Add(s);
            //}

            resCtr.createReservation(p, show, seats, reservationsID);
        }

        public List<Person> findAllPerson()
        {
        //    Person[] ps = new Person[250];
        //    int index = 0;
        //    foreach (Person p in pCtr.findAllPerson())
        //    {

        //        ps[index] = p;
        //        index++;
        //    }

        //    return ps;

            return pCtr.findAllPerson();
        }

        public void deleteReservation(int reservationsID)
        {
            resCtr.deleteReservation(reservationsID);
        }

        public List<Reservation> findAllReservations()
        {
            //Reservation[] res = new Reservation[50];
            //int index = 0;
            //foreach (Reservation r in resCtr.findAllReservations())
            //{
            //    res[index] = r;
            //    index++;
            //}
            //return res;
            return resCtr.findAllReservations();
        }

        public List<Show> findAllShows()
        {
            //Show[] show = new Show[50];
            //int index = 0;
            //foreach (Show s in sCtr.findAllShows())
            //{
            //    show[index] = s;
            //    index++;
            //}

            //return show;
            return sCtr.findAllShows();
        }

        public List<Movie> findAllMovie()
        {
            //List<Movie> movie = new List<Movie>();
            //int index = 0;
            //foreach(Movie m in sCtr.findAllMovie())
            //{
            //    movie[index] = m;
            //    index++;
            //}
            //return movie;

            return sCtr.findAllMovie();
        }

        public List<ShowRoom> findAllShowRooms()
        {
            return shCtr.findAllShowRooms();
            //List<ShowRoom> sh = new List<ShowRoom>();
            
            //foreach (ShowRoom s in shCtr.findAllShowRooms())
            //{
            //    sh.Add(s);
            //}
            //return sh;
        }




        //public void Serialize(Person p)
        //{
        //    //DataContractSerializer serializer = new DataContractSerializer(p.GetType());
        //    //BinaryFormatter bin = new BinaryFormatter();
        //    //MemoryStream memoryStream = new MemoryStream();
        //    //bin.Serialize(memoryStream, p);

        //    //serializer.WriteObject(memoryStream, p);
        //    //BufferedStream bStream = new BufferedStream(memoryStream);
        //    ////return Encoding.UTF8.GetString(memoryStream.GetBuffer());


        //}

        //public Person SerializePerson(int tlf)
        //{

        //    Person p = pCtr.findPerson(tlf);
        //    //fileName = "Person.xml";
        //    //FileStream writer = new FileStream(fileName, FileMode.Create);

        //    DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
        //    using (MemoryStream stream = new MemoryStream())
        //    {
        //        using (XmlWriter xmlWriter = XmlWriter.Create(stream))
        //        {
        //            serializer.WriteObject(xmlWriter, p);
        //            string xml = Encoding.UTF8.GetString(stream.GetBuffer());
        //            return (Person)serializer.ReadObject(XmlReader.Create(new StringReader(xml)));
        //        }
        //    }
        //    //MemoryStream memorystream = new MemoryStream();
            
            
        //    //string fileName = "Person.xml";
        //    //BinaryFormatter binFormat = new BinaryFormatter();
        //    //using (Stream fStream = new FileStream(fileName,
        //    //    FileMode.Create, FileAccess.Write, FileShare.None))
        //    //{
        //    //    binFormat.Serialize(fStream, p);
        //    //}





        //    //FileStream fs = new FileStream(fil, FileMode.Create);
        //    //DataContractSerializer ser = new
        //    //    DataContractSerializer(typeof(Person));
        //    //ser.WriteObject(fs, p);
        //    //fs.Close();


        //}

        //public void SaveAsBinaryFormat(Person objGraph, string fileName)
        //{
        //    // Save object to a file named CarData.dat in binary.
        //    BinaryFormatter binFormat = new BinaryFormatter();
        //    using (Stream fStream = new FileStream(fileName,
        //        FileMode.Create, FileAccess.Write, FileShare.None))
        //    {
        //        binFormat.Serialize(fStream, objGraph);
        //    }
        //}
    }
}
